﻿using System;
using System.Text;

using DreamCube.Foundation.Basic.Utility;

namespace DreamCube.Framework.Utilities.Report
{
    /// <summary>
    /// 报表对象
    /// </summary>
    public class ReportForm : ElementContainer
    {
        #region "字段"

        /// <summary>
        /// 引入Script文件
        /// </summary>
        private static String scriptTag = "<script src='{0}' type='text/javascript'></script>";

        /// <summary>
        /// 引入Css文件
        /// </summary>
        private static String cssTag = "<link href='{0}' rel='stylesheet' type='text/css' />";

        /// <summary>
        /// Html模板
        /// {0}:整个报表内容的CSS样式
        /// {1}:报表标题的样式
        /// {2}:报表的标题
        /// {3}:报表数据区域的样式
        /// {4}:报表的内容
        /// </summary>
        private static String html = "<table border='0' cellpadding='0' cellspacing='0' class='{0}'>" +
                                        "<tr>" +
                                            "<td class='{1}' id='td_reportTitle'>{2}</td>" +
                                        "</tr>" +
                                        "<tr>" +
                                            "<td class='{3}' id='td_reportZone'>{4}</td>" +
                                        "</tr>" +
                                     "</table><br><br>";

        #endregion

        #region "属性"

        /// <summary>
        /// 整个报表的样式类（整个报表的布局就是由一个大的table控制的）
        /// </summary>
        public String ReportFormTableStyle
        {
            get;
            set;
        }

        /// <summary>
        /// 报表标题的样式
        /// </summary>
        public String ReportFormTitleStyle
        {
            get;
            set;
        }

        /// <summary>
        /// 报表数据区域的TD样式
        /// </summary>
        public String ReportFormElementsTDStyle
        {
            get;
            set;
        }

        /// <summary>
        /// 报表的标题
        /// </summary>
        public String Title
        {
            get;
            set;
        }

        /// <summary>
        /// 报表引入的JS文件
        /// </summary>
        public String JSFiles
        {
            get;
            set;
        }

        /// <summary>
        /// 报表引入的CSS文件
        /// </summary>
        public String CSSFiles
        {
            get;
            set;
        }

        #endregion

        #region "方法"

        /// <summary>
        /// 生成Html
        /// </summary>
        /// <returns></returns>
        public override String ToHtml()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(base.ToHtml());
            String webUrl = MyWebsite.GetServerAddr();
            if (!String.IsNullOrEmpty(this.CSSFiles))
            {
                String[] cssFiles = MyString.SplitEx(this.CSSFiles, "\n");
                for (var i = 0; i < cssFiles.Length; i++)
                    sb.AppendFormat(cssTag, cssFiles[i].Replace("~", webUrl));
            }

            if (!String.IsNullOrEmpty(this.JSFiles))
            {
                String[] jsFiles = MyString.SplitEx(this.JSFiles, "\n");
                for (var i = 0; i < jsFiles.Length; i++)
                    sb.AppendFormat(scriptTag, jsFiles[i].Replace("~", webUrl));
            }
            return String.Format(html, ReportFormTableStyle, ReportFormTitleStyle, Title, ReportFormElementsTDStyle, sb.ToString());
        }

        #endregion
    }
}
